import saleService from "@/apis/sale";
import http from "@/utils/request.js"
import posterService from "@/apis/poster";

// pages/product/products/products.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    quanyi:'',
    posterList: [],
    activeTab: 0,
    categories: [],
    images: [],
    posterMap: new Map(),
    showFooter: false
  },
  startOrder(){
    wx.navigateTo({
      url: '/pages/order/detail/detail'
    }) 
  },
  initPostersData(data) {
    const rawData = data;
    const posterMap = new Map();

    // 处理原始数据
    rawData.forEach(item => {
      // 省份处理
      if (!posterMap.has(item.scname)) {
        posterMap.set(item.scname, '' );
      }
      posterMap.set(item.scname,http.baseUrl+item.image_name);
    });
    // 新增名称数组处理
    this.setData({
      activeTab: 0,
      posterList: rawData,
      posterMap,
      categories: Array.from(posterMap.keys()),
      images: Array.from(posterMap.values())
    });
  },
  onTabChange(e) {
    this.setData({
      activeTab: e.detail.index
    });
  },
  selectedModelIdChange(e) {
    const {selectedModelId}=e.detail;
    posterService.list_model(selectedModelId).then(res => {
      this.initPostersData(res.data.data)
    }) 
  },
  handlePreviewImage(e) {
    const src = e.currentTarget.dataset.src
    wx.previewImage({
      current: src,   // 当前显示图片
      urls: [src],    // 需要预览的图片列表
      fail: (err) => {
        console.error('图片预览失败:', err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app=getApp()
    wx.setKeepScreenOn({
      keepScreenOn: app.globalData.isRecording,
      success: () => console.log('常亮已启用')
    });
    console.log(app.globalData.isRecording)
    this.setData({
      isRecording: app.globalData.isRecording
    })
    //检查app.globalData中的order对象内容
    // console.log("product/products.js onLoad() app.globalData.order: "+JSON.stringify(app.globalData.order));
    saleService.get_sale('权益守护').then(res => {
      this.setData({
        quanyi: http.baseUrl+res.data.content
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const app = getApp();

    // 新增状态监听
    this.updateFooterStatus(app.globalData.order)

    app.checkToken().then(valid => {
      console.log("product/products.js onShow() token验证结果: "+valid);
    });
  },
  updateFooterStatus(order) {
    const show = order?.order_status < 1 || false
    this.setData({ showFooter: show })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    const app=getApp()
    wx.setKeepScreenOn({
      keepScreenOn: app.globalData.isRecording,
      success: () => console.log('常亮已启用')
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})